package com.dapeis.web.admin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ag.core.base.Pager;
import com.ag.core.convert.DefaultPagerSearchConvertCriteria;
import com.ag.core.exception.InvalidParamException;
import com.ag.core.exception.SysTipsException;
import com.ag.module.common.api.domain.NoticePush;
import com.ag.module.common.api.service.NoticePushService;
import com.ag.module.common.api.service.text.base.CustomXiaoMiPushNotice;
import com.ag.module.common.api.utils.AgAssert;
import com.ag.utils.CollectionUtils;
import com.ag.utils.NumberUtils;
import com.dapeis.service.JPushService;
import com.xiaomi.xmpush.server.Message;

@Controller
@RequestMapping(value = "/admin/jpush", name = "推送管理")
public class AdminJpushController extends AdminController {
    @Resource
    NoticePushService noticePushService;
    @Resource
    JPushService jpushService;

    @RequestMapping("/list")
    public String commonList(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	if (pager == null) {
	    pager = new Pager();
	}
	pager.init(NoticePush.class, new DefaultPagerSearchConvertCriteria());

	noticePushService.pager(pager);
	model.put("pager", pager);
	return doPackageURI("jpush/list");
    }

    @RequestMapping("/add")
    public String add(RedirectAttributes attr, Map<Object, Object> model) {
	return this.doPackageURI("jpush/edit");
    }

    @RequestMapping("/dosave")
    public String doSaveOrUpdate(@ModelAttribute NoticePush noticePush,
	    HttpSession session, HttpServletRequest request,
	    @RequestParam(defaultValue = "0") String passThrough,
	    RedirectAttributes attr) {

	if (NoticePush.Flag.DeviceType.IDS.equals(noticePush.getDeviceType())) {
	    if (StringUtils.isBlank(noticePush.getTerminalIds())) {
		throw new InvalidParamException("给指定用户推送消息时，用户ID集不能为空.");
	    }
	} else {
	    if (StringUtils.isNotBlank(noticePush.getTerminalIds())) {
		throw new InvalidParamException("给非指定用户推送消息时，用户ID集要为空.");
	    }
	}
	CustomXiaoMiPushNotice customXiaoMiPushNotice = new CustomXiaoMiPushNotice();
	customXiaoMiPushNotice.setContent(noticePush.getContent());
	customXiaoMiPushNotice.setTitle(noticePush.getThumbTitle());
	customXiaoMiPushNotice.setUrl(noticePush.getUrl());
	customXiaoMiPushNotice
		.setPassThrough("1".equals(passThrough) ? Message.PASS_THROUGH_PASS
			: Message.PASS_THROUGH_NOTIFICATION);

	if (StringUtils.isNotBlank(noticePush.getTerminalIds())) {
	    List<Integer> userIds = new ArrayList<Integer>();
	    String[] split = StringUtils
		    .split(noticePush.getTerminalIds(), ",");
	    for (String string : split) {
		int userId = NumberUtils.parseInt(string, -1);
		if (NumberUtils.isNotValid(userId)) {
		    logger.error("自定义ID出错,不是int型参数.{}", string);

		    continue;
		} else {
		    userIds.add(userId);
		}
	    }

	    AgAssert.isTrue(CollectionUtils.isNotEmpty(userIds),
		    SysTipsException.class, "有效的用户ID集不能为空.");
	    // noticePushService.addNoticePushAndPushFromIds(userIds,
	    // customXiaoMiPushNotice);
	    jpushService.addNoticePushAndPushFromColloctaionIds(userIds,
		    customXiaoMiPushNotice);
	} else {
	    noticePushService.addNoticePushAndPushFromDeviceType(
		    noticePush.getDeviceType(), customXiaoMiPushNotice);
	}
	setInfoMsg("操作成功！", attr);
	return redirect("list");
    }
}